﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using rInvoice.Utils;

namespace rInvoice
{
    public partial class ProductsInputPanel : Form
    {
        private ServerObject mServerObject = null;
        bool mCreatingNew = false;
        bool mIsLoading = false;
        bool mIsActive = true;

        int mCod = 0;
        int mTypID = 0;
        int mClassifierID = 0;
        int mProductID = 0;
        string mProductName = string.Empty;
        string mCopyrignt = string.Empty;
        int mCompanyProductorID = 0;
        int mProductYear = 0;
        int mProductMonth = 0;
        int mProductDay = 0;
        string mBarCod = string.Empty;
        string mProductNameShort = string.Empty;

        public bool CreateNew
        {
            get { return mCreatingNew; }
            set { mCreatingNew = value; }
        }

        public int Cod
        {
            get { return mCod; }
            set { mCod = value; }
        }

        public int TypID
        {
            get { return mTypID; }
            set { mTypID = value; }
        }

        public int ProductID
        {
            get { return mProductID; }
            set { mProductID = value; }
        }

        public int ClassifierID
        {
            get { return mClassifierID; }
            set { mClassifierID = value; }
        }

        public string ProductNume
        {
            get { return mProductName; }
            set { mProductName = value; }
        }

        public string Copyrignt
        {
            get { return mCopyrignt; }
            set { mCopyrignt = value; }
        }

        public int CompanyProductorID
        {
            get { return mCompanyProductorID; }
            set { mCompanyProductorID = value; }
        }

        public int ProductYear
        {
            get { return mProductYear; }
            set { mProductYear = value; }
        }

        public int ProductMonth
        {
            get { return mProductMonth; }
            set { mProductMonth = value; }
        }

        public int ProductDay
        {
            get { return mProductDay; }
            set { mProductDay = value; }
        }

        public string BarCod
        {
            get { return mBarCod; }
            set { mBarCod = value; }
        }

        public string ProductNameShort
        {
            get { return mProductNameShort; }
            set { mProductNameShort = value; }
        }

        public bool IsActive
        {
            get { return mIsActive; }
            set { mIsActive = value; }
        }



        public ProductsInputPanel()
        {
            InitializeComponent();
            mServerObject = new ServerObject();
        }
       
        private void cancelButton_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
        }

        private void FillProductTypeCombo()
        {
            DataTable productTypeList = mServerObject.GetClassifiersListByTypeID(UFT.Table_TypeID.NAME_ProductType);
            typeComboBox.DataSource = productTypeList;
            typeComboBox.DisplayMember = "Name";
            typeComboBox.ValueMember = "CodeID";
            typeComboBox.SelectedIndex = -1;
        }

        private void FillClassifierTypeCombo()
        {
            DataTable classifierTypeList = mServerObject.GetClassifiersListByTypeID(UFT.Table_TypeID.NAME_ClassifierID);
            classifiersComboBox.DataSource = classifierTypeList;
            classifiersComboBox.DisplayMember = "Name";
            classifiersComboBox.ValueMember = "CodeID";
            classifiersComboBox.SelectedIndex = -1;
        }

        private void FillCompanyComboBox()
        {
            bool isDeleted = false;
            DataTable companyList = mServerObject.SelectCompanyList(isDeleted);
            companyListComboBox.DataSource = companyList;
            companyListComboBox.DisplayMember = "CompanyName";
            companyListComboBox.ValueMember = "CompanyID";
            companyListComboBox.SelectedIndex = -1;
        }

        private void ProductsInputPanel_Load(object sender, EventArgs e)
        {
            mIsLoading = true;

            FillCompanyComboBox();
            FillProductTypeCombo();
            FillClassifierTypeCombo();

            if (!mCreatingNew)
            {
                codTextBox.Text = mCod.ToString();
                typeComboBox.SelectedValue = mTypID;
                classifiersComboBox.SelectedValue = mClassifierID;
                productNameTextBox.Text = mProductName;
                copyrigntTextBox.Text = mCopyrignt;
                companyListComboBox.SelectedValue = mCompanyProductorID;
                productYearTextBox.Text = mProductYear.ToString();
                productMonthTextBox.Text = mProductMonth.ToString();
                productDayTextBox.Text = mProductDay.ToString();
                barCodTextBox.Text = mBarCod;
                productNameShortTextBox.Text = mProductNameShort;
            }

            mIsLoading = false;
        }

        private void codTextBox_TextChanged(object sender, EventArgs e)
        {
            int.TryParse(codTextBox.Text, out mCod);
        }

        private void typeComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (!mIsLoading && typeComboBox.SelectedIndex != -1)
            {
                mTypID = (int)typeComboBox.SelectedValue;
            }
        }

        private void classifiersComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (!mIsLoading && classifiersComboBox.SelectedIndex != -1)
            {
                mClassifierID = (int)classifiersComboBox.SelectedValue;
            }
        }

        private void productNameTextBox_TextChanged(object sender, EventArgs e)
        {
            mProductName = productNameTextBox.Text;
        }

        private void copyrigntTextBox_TextChanged(object sender, EventArgs e)
        {
            mCopyrignt = copyrigntTextBox.Text;
        }

        private void companyListComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (!mIsLoading && companyListComboBox.SelectedIndex != -1)
            {
                int.TryParse(companyListComboBox.SelectedValue.ToString(), out mCompanyProductorID);
            }
        }

        private void productYearTextBox_TextChanged(object sender, EventArgs e)
        {
            int.TryParse(productYearTextBox.Text, out mProductYear);
        }

        private void productMonthTextBox_TextChanged(object sender, EventArgs e)
        {
            int.TryParse(productMonthTextBox.Text, out mProductMonth);
        }

        private void productDayTextBox_TextChanged(object sender, EventArgs e)
        {
            int.TryParse(productDayTextBox.Text, out mProductDay);
        }

        private void barCodTextBox_TextChanged(object sender, EventArgs e)
        {
            mBarCod = barCodTextBox.Text;
        }

        private void productNameShortTextBox_TextChanged(object sender, EventArgs e)
        {
            mProductNameShort = productNameShortTextBox.Text;
        }

        private void activeChekBox_TextChanged(object sender, EventArgs e)
        {
            mIsActive = activeChekBox.Checked;
        }

        private void saveButton_Click(object sender, EventArgs e)
        {
            bool saveResult = false;

            if (mCreatingNew)
            {
                saveResult = mServerObject.AddProduct(mCod
                                                        , mTypID
                                                        , mClassifierID
                                                        , mProductName
                                                        , mCopyrignt
                                                        , mCompanyProductorID.ToString()
                                                        , mProductYear
                                                        , mProductMonth
                                                        , mProductDay
                                                        , mBarCod
                                                        , mProductNameShort);
            }
            else
            {
                saveResult = mServerObject.UpdateProduct(mProductID
                                                        , mCod
                                                        , mTypID
                                                        , mClassifierID
                                                        , mProductName
                                                        , mCopyrignt
                                                        , mCompanyProductorID.ToString()
                                                        , mProductYear
                                                        , mProductMonth
                                                        , mProductDay
                                                        , mBarCod
                                                        , mProductNameShort
                                                        ,!mIsActive);
            }

            if (saveResult)
            {
                this.DialogResult = DialogResult.OK;
            }
            else
            {
                this.DialogResult = DialogResult.Abort;
            }

        }

    }
}
